package com.passive.lock.service.route.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.passive.lock.mapper.TRegionMapper;
import com.passive.lock.mapper.TRouteMapper;
import com.passive.lock.mapper.TRouteMapperCoustom;
import com.passive.lock.model.po.TRegion;
import com.passive.lock.model.po.TRoute;
import com.passive.lock.model.po.TRouteExample;
import com.passive.lock.model.vo.TRouteVo;
import com.passive.lock.service.route.RouteService;
import com.passive.lock.utils.DozerMapperUtils;
import com.passive.lock.utils.Result;
import com.passive.lock.utils.tree.TreeRouteUtil;

@Service("routeService")
public class RouteServiceImpl implements RouteService {
	@Resource(name = "TRegionMapper")
	private TRegionMapper regionMapper;

	@Resource(name = "TRouteMapper")
	private TRouteMapper routeMapper;

	@Resource(name = "TRouteMapperCoustom")
	private TRouteMapperCoustom routeMapperCoustom;
	

	@Override
	public Result deleteRouteList(List<Integer> tRouteids) {
		int counts = 0;
		for (Integer id : tRouteids) {
			routeMapper.deleteByPrimaryKey(id);
			counts++;
		}
		return Result.ok(counts);
	}

	@Override
	public Result deleteRoute(TRouteVo tRouteVo) {
		int count = routeMapper.deleteByPrimaryKey(tRouteVo.getRouteid());
		return Result.ok(count);
	}

	@Override
	public Result updateRoute(TRouteVo tRouteVo) {
		String mergename = null;
		if (tRouteVo.getProuteid() == null || "".equals(tRouteVo.getProuteid())) {
			mergename = regionMapper.selectByPrimaryKey(tRouteVo.getRegionid()).getMergername();
		} else {
			mergename = routeMapper.selectByPrimaryKey(tRouteVo.getProuteid()).getMergename();
		}
		mergename = mergename + "," + tRouteVo.getRoutename();
		TRoute tRoute = new TRoute();
		DozerMapperUtils.map(tRouteVo, tRoute);
		tRoute.setMergename(mergename);
		tRoute.setUpdatetime(new Date());
		routeMapper.updateByPrimaryKey(tRoute);
		return Result.ok();
	}

	@Override
	public Result addRoute(TRouteVo tRouteVo) {
		String mergename = null;
		if (tRouteVo.getProuteid() == null || "".equals(tRouteVo.getProuteid())) {
			mergename = tRouteVo.getRoutename();
		} else {
			mergename = routeMapper.selectByPrimaryKey(tRouteVo.getProuteid()).getMergename()+tRouteVo.getRoutename();
		}
		TRoute tRoute = new TRoute();
		DozerMapperUtils.map(tRouteVo, tRoute);
		tRoute.setMergename(mergename);
		tRoute.setCreatetime(new Date());
		routeMapper.insert(tRoute);
		return Result.ok();
	}

	@Override
	public List<TRouteVo> routeList(Integer regionid, String routeName, String deptid, Integer prouteid, int offset,
			Integer limit) {
		List<TRoute> list = routeMapperCoustom.routeList(regionid, prouteid, routeName,deptid,offset, limit);
		
		List<TRouteVo> listvo = new ArrayList<TRouteVo>();
		for (TRoute tRoute : list) {
			TRouteVo vo = new TRouteVo();
			DozerMapperUtils.map(tRoute, vo);

			//获得国省市县等
			TRegion tRegion= regionMapper.selectByPrimaryKey(vo.getRegionid());
			String [] stringRegion= tRegion.getMergername().split(","); 
			if(stringRegion.length>0){
				vo.setChinese(stringRegion[0]);
			}
			if(stringRegion.length>1){
				vo.setProname(stringRegion[1]);
			}
			if(stringRegion.length>2){
				vo.setCityname(stringRegion[2]);
			}
			if(stringRegion.length>3){
				vo.setArename(stringRegion[3]);
			}
			if(stringRegion.length>4){
				vo.setCountry(stringRegion[4]);
			}
			if(stringRegion.length>5){
				vo.setVillage(stringRegion[5]);
			}
			if(stringRegion.length>6){
				vo.setCustom(stringRegion[6]);
			}
			
			String [] stringRoute= vo.getMergename().split(","); 
			if(stringRoute.length>0){
				vo.setRouteA(stringRoute[0]);
			}
			if(stringRoute.length>1){
				vo.setRouteB(stringRoute[1]);
			}
			if(stringRoute.length>2){
				vo.setRouteC(stringRoute[2]);
			}
			
			listvo.add(vo);
		}
		
		return listvo;
	}

	@Override
	public int routeListCount(Integer regionid, String routeName, String deptid, Integer prouteid) {
		int count = routeMapperCoustom.routeListCount(regionid, prouteid, routeName,deptid);
		return count;
	}

	@Override
	public Result queryrouteList(String groupid,Integer prouteid, Integer regionid) {
		TRouteExample example = new TRouteExample();
		TRouteExample.Criteria Criteria = example.createCriteria();
		if (prouteid != null) {
			Criteria.andProuteidEqualTo(prouteid);
		}
		if(regionid !=null){
			Criteria.andRegionidEqualTo(regionid);
		}
		List<TRoute> list = routeMapper.selectByExample(example);
		return Result.ok(list);
	}

	@Override
	public Result routeTree(Integer regionid,String groupid) {
		
		List<TRoute> list = routeMapperCoustom.routeTree(regionid,groupid,null);
		List<TRouteVo> listTree = new ArrayList<TRouteVo>();
		for (TRoute tree : list) {
			TRouteVo tRouteVo = new TRouteVo();
			DozerMapperUtils.map(tree, tRouteVo);
			listTree.add(tRouteVo);
		}
		
		List<TRouteVo> newTreeDataList = TreeRouteUtil.getfatherNode(listTree);
		return Result.ok(newTreeDataList);
	}
}
